<template>
  <div>
    <el-form :model="addData" ref="addform" label-width="100px" size="small">
      <el-form-item label="快递公司">
        <el-select v-model="addData.express_id" placeholder="请选择" style="width: 80%">
          <el-option v-for="(item,index) in expList" :key="index" :label="item" :value="index+1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="面单客户账号">
        <el-input v-model="addData.customer_name" placeholder="请输入" style="width: 80%"/>
      </el-form-item>
      <el-form-item label="面单密码">
        <el-input v-model="addData.customer_pwd" placeholder="请输入" style="width: 80%"/>
      </el-form-item>
      <el-form-item label="月结编码">
        <el-input v-model="addData.month_code" placeholder="请输入" style="width: 80%"/>
      </el-form-item>
      <el-form-item label="网点编码">
        <el-input v-model="addData.dot_code" placeholder="请输入" style="width: 80%"/>
      </el-form-item>
      <el-form-item label="网点名称">
        <el-input v-model="addData.dot_name" placeholder="请输入" style="width: 80%"/>
      </el-form-item>
      <el-form-item label="发件人公司">
        <el-input v-model="addData.company" placeholder="请输入" style="width: 80%"/>
      </el-form-item>
      <el-form-item label="发件人姓名">
        <el-input v-model="addData.name" placeholder="请输入" style="width: 80%"/>
      </el-form-item>
      <el-form-item label="发件人电话">
        <el-input v-model="addData.tel" placeholder="请输入" style="width: 80%"/>
      </el-form-item>
      <el-form-item label="发件人手机">
        <el-input v-model="addData.phone" placeholder="请输入" style="width: 80%"/>
      </el-form-item>
      <el-form-item label="发件人邮编">
        <el-input v-model="addData.post_code" placeholder="请输入" style="width: 80%"/>
      </el-form-item>
      <el-form-item label="发件人地区">
        <el-cascader :options="options" v-model="selectedOptions" @change="handleChange" style="width:250px" placeholder="请选择省市区"></el-cascader>
      </el-form-item>
      <el-form-item label="发件人详细地址">
        <el-input v-model="addData.addr" placeholder="请输入" style="width: 80%"/>
      </el-form-item>
      <el-form-item label="托寄物">
        <el-input v-model="addData.towing_goods" placeholder="请输入" style="width: 80%"/>
      </el-form-item>
      <el-form-item label="状态">
        <el-switch v-model="addData.status" active-color="#13ce66" active-value="1" inactive-value="0"></el-switch>
      </el-form-item>
    </el-form>

    <div style="text-align:center">
      <el-button type="primary" size="small" style="padding:8px 30px;" @click="submit">提 交</el-button>
    </div>
  </div>
</template>

<script>
import { regionData, CodeToText } from 'element-china-area-data'
import { postExpDoc, putExpDoc } from '@/api/setting'

export default {
  props: {
    addData: {
      type: Object,
      required: true
    },
    isAdd: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      expList: ['顺丰快递','百世快递','中通快递','申通快递','圆通快递','韵达快递','邮政快递包裹','EMS','天天快递','京东快递','优速快递','德邦快递','宅急送','安能物流','安能快运',
        '百世快运','承诺达','德邦快运','国通快递','天地华宇','京东快运','佳吉快运','跨越速运','龙邦快递','品骏快递','速尔快递','腾林物流','全一快递','温通物流','信丰物流','远成快运','邮政国内标快','中通快运','韵达国际'],
      selectedOptions: [],
      options: regionData,
    }
  },
  methods: {
    /**
     * 提交
     */
    submit() {
      this.$refs.addform.validate((valid) => {
        if(valid) {
          if(this.isAdd){
            postExpDoc(this.addData).then(response => {
              if(response.status === 200){
                this.$emit('success');
                this.$message.success('添加成功！');
              }else{
                this.$message.error(response.message);
              }
            })
          }else{
            putExpDoc(this.addData).then(response => {
              if(response.status === 200){
                this.$emit('success');
                this.$message.success('修改成功！');
              }else{
                this.$message.error(response.message);
              }
            })
          }
        }
      })    
    },
    /**
     * 省市联动
     */
    handleChange (areaCodeList) {
      this.addData.province_name = CodeToText[areaCodeList[0]];

      this.addData.city_name = CodeToText[areaCodeList[1]];

      this.addData.area_name = CodeToText[areaCodeList[2]];
    },
  },
}
</script>